const baseUrl = 'https://flower.kmmyxb.cn'

export const request = function(params) {
    let headers = {
        'content-type': 'application/json',
        'Accept': 'application/json, text/javascript, */*; q=0.01'
    }
    let Authorization = wx.getStorageSync('Authorization')
    if (Authorization) {
        headers["Authorization"] = Authorization
    }
    wx.showLoading({
        title: params.loading || '加载中...',
        mask: true
    })
    wx.request({
        url: baseUrl + params.url,
        header: headers,
        method: params.method || "POST",
        data: params.data,
        success: (res) => {
            if (res.statusCode == 200) {
                if (res.data.code == 0) {
                    params.success(res);
                }
            } else if (res.statusCode == 401) {
                wx.showToast({
                    title: '请先登录',
                    icon: 'none'
                })
            } else {
                wx.showToast({
                    title: res.data || "网络异常",
                    icon: 'none'
                })
            }
        },
        fail: (err) => {
            if (params.fail) {
                return params.fail(err)
            }
            wx.showToast({
                title: '网络异常 ' + err.errMsg,
                icon: 'none'
            })
        },
        complete: (data) => {
            wx.hideLoading();
            params.complete && params.complete(data);
        }
    })
}